

var bdPass = bdPass || {};

(function(){

var baidu={version:"1-0-0",emptyFn:function(){}};baidu.isString=function(a){return(typeof a=="object"&&a&&a.constructor==String)||typeof a=="string"
};baidu.sio=baidu.sio||{};baidu.url=baidu.url||{};baidu.string=baidu.string||{};baidu.string.escapeReg=function(a){return String(a).replace(new RegExp("([.*+?^=!:\x24{}()|[\\]/\\\\])","g"),"\\\x241")
};baidu.url.parseQuery=function(j,n){if(n){var l=new RegExp("(^|&|\\?|#)"+baidu.string.escapeReg(n)+"=([^&]*)(&|\x24)","");
var k=j.match(l);if(k){return k[2]}}else{if(j){j=j.substr(j.indexOf("?")+1,j.length);var g=j.split("&");var h={},e=[],d,c,m;
for(var f=0;f<g.length;f++){d=g[f].split("=");c=d[0],m=d[1];if(c===""){continue}if(typeof h[c]=="undefined"){h[c]=m}else{if(baidu.isString(h[c])){continue
}else{h[c].push(m)}}}return h}}return null};baidu.sio.get=function(d,a){if(!d||!baidu.isString(d)){return null}var g=baidu.url.parseQuery(d,"callback");
var f=document.createElement("SCRIPT");if(!g){var c=false;f.onreadystatechange=f.onload=function(){if(c){return false}if(typeof f.readyState==="undefined"||f.readyState=="loaded"||f.readyState=="complete"){c=true;
try{a()}finally{if(f&&f.parentNode){f.parentNode.removeChild(f)}f.onreadystatechange=null;f.onload=null;f=null}}}}else{var b="CB"+Math.floor(Math.random()*2147483648).toString(36);
d=d.replace(/(&|\?)callback=([^&]*)/,"\x241callback="+b);window[b]=function(){try{var h=(a||window[g]);h.apply(null,arguments)
}finally{if(f&&f.parentNode){f.parentNode.removeChild(f)}f=null;window[b]=null}}}f.src=d;f.type="text/javascript";var e=document.getElementsByTagName("HEAD")[0];
e.insertBefore(f,e.firstChild)};



    if( bdPass.ShellTemplate){
	return;
    }


    var shell = function(){
	var bdTarget,bdConfig,bdType,json;
	var st;
	var busyFlag = false;
	var preUrl = 'http://passport.baidu.com/api/?';

	var errorHash = {
	    login:{
		'-1': 'no-token'
	    },
	    reg:{
		'-1': 'no-token'
	    },
	    lockmail:{
		'1': 'no-token',
		'2': 'need-login',
		'20': 'system-error'
	    },
	    lockphone: {
		'1': 'no-token',
		'2': 'need-login',
		'20': 'system-error'
	    }
	};	


	var packUrl = function(type , config){
	    var url = [];
        if(config.code != undefined) {
            url.push('code=' + config.code);
            url.push('oe=gbk');
        }
        url.push('tpl=' + config.tpl );
	    url.push( 'callback=' + 'getPassJs' );
        return (preUrl + type + '&' + url.join('&'));
	}; 

	var getPassJs = function(v , obj){
	    if( typeof v == 'string' ){
		v = eval(v);
	    }
	    json = v;
        
        if(json.error_code) {
        }else if( errorHash[bdType][json.error_no] ){
		var error_no = errorHash[bdType][json.error_no];

		switch( error_no ){
		case 'need-login':  bdConfig.onNotLogin && bdConfig.onNotLogin();return;
		case 'no-token': bdConfig.onSystemError && bdConfig.onSystemError();return;
		case 'system-error': bdConfig.onSystemError && bdConfig.onSystemError();break;
		}
		
	    }
	    
	    var link = json.jslink;
	    baidu.sio.get(link , renderCallback);
	};

	var renderCallback = function(){
	    var template = bdType.charAt(0).toUpperCase() + bdType.slice(1) + 'Template';
	    bdPass[template]['render'](json , bdTarget , bdConfig);
	    busyFlag = false;
	};

	var callJson = function(){
	    var url = packUrl( bdType , bdConfig );
        baidu.sio.get( url , getPassJs );
	};

	var init = function(type,target,config){
	    bdType =type , bdTarget = target , bdConfig = config ;
	    callJson();

	};


	return{
	    render: function(type , target , config ){
		
        if(!busyFlag){
		    busyFlag = true;
		    clearTimeout(st);
		    init(type,target,config);
		}else{
		    st = setTimeout( function(){
			shell.render(type,target,config);
		    } , 300 );
		}


		if( !config.tpl ){
		    alert('需要tpl');
		}


	    }
	}
    }();
    
    bdPass.ShellTemplate = shell;

})();
